<template>
  <div class="search-page">
    <div class="container">
      <!-- 搜索筛选区域 -->
      <div class="search-filters">
        <div class="filter-row">
          <el-input
            v-model="filters.keyword"
            placeholder="搜索目的地、房源名称"
            size="large"
            class="search-input"
            @keyup.enter="handleSearch"
          >
            <template #append>
              <el-button @click="handleSearch">
                <el-icon><Search /></el-icon>
              </el-button>
            </template>
          </el-input>
        </div>
        
        <div class="filter-row">
          <el-select v-model="filters.priceRange" placeholder="价格范围" class="filter-item">
            <el-option label="不限" value="" />
            <el-option label="¥0-200" value="0-200" />
            <el-option label="¥200-500" value="200-500" />
            <el-option label="¥500-1000" value="500-1000" />
            <el-option label="¥1000以上" value="1000+" />
          </el-select>
          
          <el-select v-model="filters.propertyType" placeholder="房源类型" class="filter-item">
            <el-option label="不限" value="" />
            <el-option label="整租" value="entire" />
            <el-option label="合租" value="shared" />
            <el-option label="单间" value="room" />
          </el-select>
          
          <el-select v-model="filters.amenities" placeholder="设施" class="filter-item">
            <el-option label="不限" value="" />
            <el-option label="WiFi" value="wifi" />
            <el-option label="空调" value="ac" />
            <el-option label="厨房" value="kitchen" />
            <el-option label="停车位" value="parking" />
          </el-select>
          
          <el-select v-model="filters.sortBy" placeholder="排序方式" class="filter-item">
            <el-option label="推荐排序" value="recommend" />
            <el-option label="价格从低到高" value="price-asc" />
            <el-option label="价格从高到低" value="price-desc" />
            <el-option label="评分从高到低" value="rating-desc" />
          </el-select>
        </div>
      </div>

      <!-- 搜索结果 -->
      <div class="search-results">
        <div class="results-header">
          <h2>找到 {{ searchResults.length }} 个房源</h2>
          <div class="view-toggle">
            <el-button-group>
              <el-button :type="viewMode === 'grid' ? 'primary' : ''" @click="viewMode = 'grid'">
                <el-icon><Grid /></el-icon>
              </el-button>
              <el-button :type="viewMode === 'list' ? 'primary' : ''" @click="viewMode = 'list'">
                <el-icon><List /></el-icon>
              </el-button>
            </el-button-group>
          </div>
        </div>

        <!-- 网格视图 -->
        <div v-if="viewMode === 'grid'" class="property-grid">
          <div
            v-for="property in searchResults"
            :key="property.id"
            class="property-card"
            @click="$router.push(`/property/${property.id}`)"
          >
            <div class="property-image">
              <img :src="property.image" :alt="property.title" />
              <div class="property-badge" v-if="property.isHot">热门</div>
              <div class="favorite-btn" @click.stop="toggleFavorite(property.id)">
                <el-icon :class="{ 'is-favorite': property.isFavorite }">
                  <Star />
                </el-icon>
              </div>
            </div>
            <div class="property-info">
              <h3>{{ property.title }}</h3>
              <p class="property-location">
                <el-icon><Location /></el-icon>
                {{ property.location }}
              </p>
              <div class="property-rating">
                <el-rate v-model="property.rating" disabled show-score />
                <span class="review-count">({{ property.reviewCount }}条评价)</span>
              </div>
              <div class="property-amenities">
                <el-tag v-for="amenity in property.amenities" :key="amenity" size="small">
                  {{ amenity }}
                </el-tag>
              </div>
              <div class="property-price">
                <span class="price">¥{{ property.price }}</span>
                <span class="unit">/晚</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 列表视图 -->
        <div v-else class="property-list">
          <div
            v-for="property in searchResults"
            :key="property.id"
            class="property-list-item"
            @click="$router.push(`/property/${property.id}`)"
          >
            <div class="property-image">
              <img :src="property.image" :alt="property.title" />
              <div class="property-badge" v-if="property.isHot">热门</div>
            </div>
            <div class="property-content">
              <div class="property-header">
                <h3>{{ property.title }}</h3>
                <div class="favorite-btn" @click.stop="toggleFavorite(property.id)">
                  <el-icon :class="{ 'is-favorite': property.isFavorite }">
                    <Star />
                  </el-icon>
                </div>
              </div>
              <p class="property-location">
                <el-icon><Location /></el-icon>
                {{ property.location }}
              </p>
              <div class="property-rating">
                <el-rate v-model="property.rating" disabled show-score />
                <span class="review-count">({{ property.reviewCount }}条评价)</span>
              </div>
              <div class="property-amenities">
                <el-tag v-for="amenity in property.amenities" :key="amenity" size="small">
                  {{ amenity }}
                </el-tag>
              </div>
              <div class="property-description">
                {{ property.description }}
              </div>
            </div>
            <div class="property-price-section">
              <div class="property-price">
                <span class="price">¥{{ property.price }}</span>
                <span class="unit">/晚</span>
              </div>
              <el-button type="primary" @click.stop="$router.push(`/booking/${property.id}`)">
                立即预订
              </el-button>
            </div>
          </div>
        </div>

        <!-- 分页 -->
        <div class="pagination-container">
          <el-pagination
            v-model:current-page="currentPage"
            :page-size="pageSize"
            :total="totalResults"
            layout="prev, pager, next, jumper"
            @current-change="handlePageChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

// 搜索筛选条件
const filters = reactive({
  keyword: '',
  priceRange: '',
  propertyType: '',
  amenities: '',
  sortBy: 'recommend'
})

// 视图模式
const viewMode = ref('grid')

// 分页
const currentPage = ref(1)
const pageSize = ref(12)
const totalResults = ref(0)

// 模拟搜索结果数据
const searchResults = ref([
  {
    id: 1,
    title: '温馨小院民宿',
    location: '北京·朝阳区',
    price: 299,
    rating: 4.8,
    reviewCount: 128,
    image: 'https://images.unsplash.com/photo-1566073771259-6a8506099945?w=400&h=300&fit=crop',
    isHot: true,
    isFavorite: false,
    amenities: ['WiFi', '空调', '厨房'],
    description: '位于朝阳区的温馨小院，环境优雅，设施齐全，是您在北京的理想住宿选择。'
  },
  {
    id: 2,
    title: '海边度假别墅',
    location: '青岛·市南区',
    price: 599,
    rating: 4.9,
    reviewCount: 89,
    image: 'https://images.unsplash.com/photo-1571896349842-33c89424de2d?w=400&h=300&fit=crop',
    isHot: true,
    isFavorite: true,
    amenities: ['WiFi', '空调', '停车位', '厨房'],
    description: '面朝大海的度假别墅，享受海风拂面的惬意时光，是您放松身心的绝佳选择。'
  },
  {
    id: 3,
    title: '山间木屋',
    location: '杭州·西湖区',
    price: 399,
    rating: 4.7,
    reviewCount: 156,
    image: 'https://images.unsplash.com/photo-1582719478250-c89cae4dc85b?w=400&h=300&fit=crop',
    isHot: false,
    isFavorite: false,
    amenities: ['WiFi', '空调'],
    description: '隐藏在西湖山间的木屋，远离城市喧嚣，享受宁静的自然环境。'
  },
  {
    id: 4,
    title: '城市公寓',
    location: '上海·浦东新区',
    price: 199,
    rating: 4.6,
    reviewCount: 203,
    image: 'https://images.unsplash.com/photo-1522708323590-d24dbb6b0267?w=400&h=300&fit=crop',
    isHot: false,
    isFavorite: false,
    amenities: ['WiFi', '空调', '停车位'],
    description: '现代化的城市公寓，交通便利，设施完善，适合商务出行。'
  },
  {
    id: 5,
    title: '古韵四合院',
    location: '北京·东城区',
    price: 799,
    rating: 4.9,
    reviewCount: 67,
    image: 'https://images.unsplash.com/photo-1544984243-ec57ea16fe25?w=400&h=300&fit=crop',
    isHot: true,
    isFavorite: true,
    amenities: ['WiFi', '空调', '厨房'],
    description: '传统四合院改造的精品民宿，体验老北京的文化韵味。'
  },
  {
    id: 6,
    title: '田园农庄',
    location: '成都·郫都区',
    price: 249,
    rating: 4.5,
    reviewCount: 94,
    image: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=300&fit=crop',
    isHot: false,
    isFavorite: false,
    amenities: ['WiFi', '停车位', '厨房'],
    description: '田园风光的农庄民宿，体验乡村生活的宁静与美好。'
  }
])

// 初始化搜索条件
onMounted(() => {
  if (route.query.keyword) {
    filters.keyword = route.query.keyword
  }
  if (route.query.checkIn) {
    // 处理入住日期
  }
  if (route.query.checkOut) {
    // 处理退房日期
  }
  if (route.query.guests) {
    // 处理人数
  }
  
  totalResults.value = searchResults.value.length
})

// 搜索处理
const handleSearch = () => {
  // 这里可以调用API进行搜索
  console.log('搜索条件:', filters)
  // 模拟搜索逻辑
  currentPage.value = 1
}

// 切换收藏状态
const toggleFavorite = (propertyId) => {
  const property = searchResults.value.find(p => p.id === propertyId)
  if (property) {
    property.isFavorite = !property.isFavorite
  }
}

// 分页处理
const handlePageChange = (page) => {
  currentPage.value = page
  // 这里可以调用API获取对应页面的数据
}
</script>

<style scoped>
.search-page {
  min-height: 100vh;
  background: #f8f9fa;
  padding: 20px 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.search-filters {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
}

.filter-row {
  display: flex;
  gap: 15px;
  align-items: center;
  flex-wrap: wrap;
}

.filter-row:first-child {
  margin-bottom: 20px;
}

.search-input {
  flex: 1;
  min-width: 300px;
}

.filter-item {
  min-width: 150px;
}

.search-results {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.results-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30px;
  border-bottom: 1px solid #eee;
}

.results-header h2 {
  color: #333;
  font-size: 1.5rem;
}

.property-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 30px;
  padding: 30px;
}

.property-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
}

.property-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.property-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.property-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.property-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background: #ff4757;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
}

.favorite-btn {
  position: absolute;
  top: 10px;
  left: 10px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
}

.favorite-btn:hover {
  background: white;
}

.favorite-btn .el-icon {
  font-size: 18px;
  color: #ddd;
  transition: color 0.3s;
}

.favorite-btn .is-favorite {
  color: #ff4757;
}

.property-info {
  padding: 20px;
}

.property-info h3 {
  font-size: 1.2rem;
  margin-bottom: 8px;
  color: #333;
}

.property-location {
  color: #666;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.property-rating {
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.review-count {
  color: #666;
  font-size: 0.9rem;
}

.property-amenities {
  margin-bottom: 15px;
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
}

.property-price {
  display: flex;
  align-items: baseline;
  gap: 5px;
}

.price {
  font-size: 1.5rem;
  font-weight: bold;
  color: #ff4757;
}

.unit {
  color: #666;
  font-size: 0.9rem;
}

.property-list {
  padding: 30px;
}

.property-list-item {
  display: flex;
  gap: 20px;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 12px;
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

.property-list-item:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border-color: #409eff;
}

.property-list-item .property-image {
  width: 200px;
  height: 150px;
  flex-shrink: 0;
}

.property-content {
  flex: 1;
}

.property-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 10px;
}

.property-header h3 {
  font-size: 1.3rem;
  color: #333;
  margin: 0;
}

.property-description {
  color: #666;
  margin-top: 10px;
  line-height: 1.5;
}

.property-price-section {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-between;
  min-width: 150px;
}

.pagination-container {
  padding: 30px;
  text-align: center;
  border-top: 1px solid #eee;
}

@media (max-width: 768px) {
  .filter-row {
    flex-direction: column;
    align-items: stretch;
  }
  
  .search-input {
    min-width: auto;
  }
  
  .property-grid {
    grid-template-columns: 1fr;
    padding: 20px;
  }
  
  .property-list-item {
    flex-direction: column;
  }
  
  .property-list-item .property-image {
    width: 100%;
    height: 200px;
  }
  
  .property-price-section {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-top: 15px;
  }
}
</style>


